#pragma once
#include <vector>
#include "kernel/fePolyline.h"

typedef fePolyline<double>::V2 Point;

class fePolylineTriangulation
{
	std::vector<fePolyline<double>::V2> Points;
	
	std::vector<int>	Tris;
	std::vector<double> Areas;
	fePolyline<double> *Poly;
	int Direction;
	bool IsNew;

	void DetermineDirection();


private:
	void TesselateTri(double iMaxSide, Point *iTri, Point *oTris, int *oTriNum);


public:
	void	Init(fePolyline<double> *iLine ){ if(iLine != Poly) {IsNew = true;} else{ IsNew = false; } Poly = iLine; }
	void	Triangulate();
	void	Tesselate(double iMaxSide);
	void	FillAreas();
	double	GetArea(int iIndex) { return Areas[iIndex]; }
	int		GetTriNum() { return (int)Tris.size()/3; }
	void	GetTri(int iIndex, Point &oP1, Point &oP2, Point &oP3)
			{ oP1 = Points[Tris[3*iIndex]]; oP2 = Points[Tris[3*iIndex+1]]; oP3 = Points[Tris[3*iIndex+2]]; }


	//-----------------------------------------------------------------------------------------------
	static bool IsInTriangle( Point &iP, Point &iP1, Point &iP2, Point &iP3 );
	

	//-----------------------------------------------------------------------------------------------
	//cross product
	static double Cp( Point &iA, Point &iB, Point &iC)
	{ return (iA.X*iB.Y - iA.X*iC.Y - iB.X*iA.Y + iB.X*iC.Y + iC.X*iA.Y - iC.X*iB.Y ); }


	//-----------------------------------------------------------------------------------------------
	//dot product
	static double Dp( Point &iA, Point &iB, Point &iC )
	{ return( (iC.X - iB.X)*(iA.X - iB.X) + (iC.Y - iB.Y)*(iA.Y - iB.Y) ); }


	//-----------------------------------------------------------------------------------------------
	static double LenSq(Point &iA, Point &iB)
	{
		return Dp(iB, iA, iB);
	}
};